package com.fany.fastsearcher.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import com.fany.fastsearcher.R;
import com.fany.utils.ConfigUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class FileSearchActivity extends ListViewActivity implements TextWatcher
{
    private boolean _append;
    private ArrayList<HashMap<String, Object>> _items = new ArrayList<HashMap<String, Object>>();
    private SimpleAdapter _adapter;
    private FileSearchDataController _dataController;
    private EditText _txtFilter;
    private boolean _forceReload;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        _txtFilter = (EditText) findViewById(R.id.text_search_filter);
        _txtFilter.addTextChangedListener(this);

        _dataController = FileSearchDataController.loadDataController(FileSearchActivity.this);
        FileSearchRotateMenuController.loadRotateMenuController(FileSearchActivity.this);
        _forceReload = true;
        updateDataPeriodically();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        _forceReload = true;
        boolean needReloadData = ConfigUtils.getInstance().getNeedReloadCache();
        if (needReloadData)
        {
            _dataController.loadData();
        }
    }

    private Handler _searchHandler;

    private void updateDataPeriodically()
    {
        final Thread thread = new Thread()
        {
            @Override
            public void run()
            {
                _dataController.initData(_items, _forceReload);
                _forceReload = false;
                if (_adapter == null)
                {
                    _adapter = new SimpleAdapter(FileSearchActivity.this, _items, R.layout.filesearch_liststyle,
                            new String[]{"filename", "fullpath"}, new int[]{R.id.text_filename, R.id.text_filedetail});
                    getListView().setAdapter(_adapter);
                }
                else
                {
                    _adapter.notifyDataSetChanged();
                }
                _searchHandler.sendEmptyMessage(0);
            }
        };

        _searchHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                postDelayed(thread, 500);
            }
        };
        _searchHandler.postDelayed(thread, 500);
    }

    // TextWatcher
    @Override
    public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3)
    {
    }

    @Override
    public void afterTextChanged(Editable s)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3)
    {
        _dataController.setFilter(s.toString().trim());
        _append = false;
    }

    @Override
    protected void onScrollToEnd()
    {
        _append = true;
        _dataController.appendData(_items);
        _adapter.notifyDataSetChanged();
    }

    @Override
    protected int getListViewID()
    {
        return R.id.list_files;
    }

    @Override
    protected int getContentViewID()
    {
        return R.layout.filesearch_activity;
    }

    @Override
    protected void onItemClick(int index)
    {
        String path = (String) _items.get(index).get("fullpath");
        Intent intent = new Intent(this, FileListActivity.class);
        intent.putExtra("path", path);
        startActivity(intent);
    }

    public void refreshFiles()
    {
        new AlertDialog.Builder(this).setTitle("提示").setMessage("你确认要重新装载数据吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        _dataController.loadData();
                    }
                }).setNegativeButton("取消", null).show();
    }

    public void settings()
    {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    public void searchFiles(String extension)
    {
        _txtFilter.setText("*" + extension);
    }
}
